<?php

declare(strict_types=1);

namespace app\common\service;

use think\facade\Request;
use app\common\model\GmTgczlogsTqk as M;
use app\common\validate\GmTgczlogsTqk as V;

class GmTgczlogsTqk
{
    // 添加
    public static function goAdd($data)
    {
        //验证
        $validate = new V;
        if (!$validate->scene('add')->check($data))
            return ['msg' => $validate->getError(), 'code' => 201];

        try {
            $orderNumber = time() . rand(1000, 9999);
            $orderNumber = "T" . $orderNumber;
            $data["cztime"] = date('Y-m-d H:i:s');
            $data["ddcode"] = $orderNumber;
            $data["cztype"] = "alipay";
            $data["status"] = "1";
            //一套正常月卡
            if ($data["tqktype"] == "100020") {
                $onet = ["50", "100", "300"];
                foreach ($onet as $value) {
                    $data["jine"] = $value;
                    $orderNumber = time() . rand(1000, 9999);
                    $orderNumber = "T" . $orderNumber;
                    $data["cztime"] = date('Y-m-d H:i:s');
                    $data["ddcode"] = $orderNumber;
                    $data["tqktype"] = $value;
                    $data["symonth"] = "30";
                    M::create($data);
                    M::accrualPlayerRmb($data['actorname'], $data['serverid'], $value);
                }
                M::SendEmail($data['serverid'], "特权卡发放提示！", "您的特权卡已发放，请注意查收！", $data['actorid']);
                //一套月卡(不给立得)
            } else if ($data["tqktype"] == "100010") {
                $onet = ["50", "100", "300"];
                foreach ($onet as $value) {
                    $data["jine"] = $value;
                    $orderNumber = time() . rand(1000, 9999);
                    $orderNumber = "T" . $orderNumber;
                    $data["cztime"] = date('Y-m-d H:i:s');
                    $data["ddcode"] = $orderNumber;
                    $data["tqktype"] = $value;
                    $data["symonth"] = "30";
                    M::create($data);
                }
                M::SendEmail($data['serverid'], "特权卡发放提示！", "您的特权卡已发放，请注意查收！", $data['actorid']);
                //一套月卡(新六卡 给立得)
            } else if ($data["tqktype"] == "100030" || $data["tqktype"] == "100060") {
                $onet = [];
                if ($data["tqktype"] == "100030") {
                    $onet = ["50", "100", "300", "998", "1998", "4998"];
                } else {
                    $onet = ["998", "1998", "4998"];
                }
                foreach ($onet as $value) {
                    $data["jine"] = $value;
                    $orderNumber = time() . rand(1000, 9999);
                    $orderNumber = "T" . $orderNumber;
                    $data["cztime"] = date('Y-m-d H:i:s');
                    $data["ddcode"] = $orderNumber;
                    $data["tqktype"] = $value;
                    $data["symonth"] = "30";
                    M::create($data);

                    if ($value == "998" || $value == "1998" || $value == "4998") {
                        if ($value == "998") {
                            M::accrualPlayerRmb($data['actorname'], $data['serverid'], '500');
                        } else if ($value == "1998") {
                            M::accrualPlayerRmb($data['actorname'], $data['serverid'], '1000');
                        } else if ($value == "4998") {
                            M::accrualPlayerRmb($data['actorname'], $data['serverid'], '2500');
                        }
                        M::sendTQKEmail($data['serverid'], $data['actorid'], $data["tqktype"], 1, $data["actorname"]);
                    } else {
                        M::accrualPlayerRmb($data['actorname'], $data['serverid'], $value);
                    }
                }
                M::SendEmail($data['serverid'], "特权卡发放提示！", "您的特权卡已发放，请注意查收！", $data['actorid']);
                //一套月卡(新六卡 不给立得)
            } else if ($data["tqktype"] == "100040" || $data["tqktype"] == "100050") {
                $onet = [];
                if ($data["tqktype"] == "100040") {
                    $onet = ["50", "100", "300", "998", "1998", "4998"];
                } else {
                    $onet = ["998", "1998", "4998"];
                }
                foreach ($onet as $value) {
                    $data["jine"] = $value;
                    $orderNumber = time() . rand(1000, 9999);
                    $orderNumber = "T" . $orderNumber;
                    $data["cztime"] = date('Y-m-d H:i:s');
                    $data["ddcode"] = $orderNumber;
                    $data["tqktype"] = $value;
                    $data["symonth"] = "30";
                    M::create($data);
                }
                M::SendEmail($data['serverid'], "特权卡发放提示！", "您的特权卡已发放，请注意查收！", $data['actorid']);
                //盲盒
            } else if ($data["tqktype"] == "10011") {
                //是否多选  1是  2否
                if ($data["ismanyvalue"] == "1") {
                    $coin = (400 * intval($data["cardnum"])) . '';
                    $data["jine"] = (50 * intval($data["cardnum"])) . '';
                    $data["symonth"] = (30 * intval($data["cardnum"])) . '';
                }else{
                    $coin = 400;
                    $data["jine"] = "50";
                    $data["symonth"] = "30";
                }
                $orderNumber = time() . rand(1000, 9999);
                $orderNumber = "Z" . $orderNumber;
                $data["cztime"] = date('Y-m-d H:i:s');
                $data["ddcode"] = $orderNumber;
                $data["tqktype"] = "10011";
                M::create($data);
                M::SendZadanNum($data['serverid'],$data['actorid'],$coin);
                M::SendEmail($data['serverid'], "盲盒特权卡发放！", "您的盲盒月卡已发放，请注意查收！", $data['actorid']);
            }else if ($data["tqktype"] == "10021") {
                if ($data["ismanyvalue"] == "1") {
                    $coin = (800 * intval($data["cardnum"])) . '';
                    $data["jine"] = (100 * intval($data["cardnum"])) . '';
                    $data["symonth"] = (30 * intval($data["cardnum"])) . '';
                }else{
                    $coin = 800;
                    $data["jine"] = "100";
                    $data["symonth"] = "30";
                }
                $orderNumber = time() . rand(1000, 9999);
                $orderNumber = "Z" . $orderNumber;
                $data["cztime"] = date('Y-m-d H:i:s');
                $data["ddcode"] = $orderNumber;
                $data["tqktype"] = "10021";
                M::create($data);
                M::SendZadanNum($data['serverid'],$data['actorid'],$coin);
                M::SendEmail($data['serverid'], "盲盒特权卡发放！", "您的盲盒月卡已发放，请注意查收！", $data['actorid']);
            }else if ($data["tqktype"] == "10031") {
                if ($data["ismanyvalue"] == "1") {
                    $coin = (1600 * intval($data["cardnum"])) . '';
                    $data["jine"] = (200 * intval($data["cardnum"])) . '';
                    $data["symonth"] = (30 * intval($data["cardnum"])) . '';
                }else{
                    $coin = 1600;
                    $data["jine"] = "200";
                    $data["symonth"] = "30";
                }
                $orderNumber = time() . rand(1000, 9999);
                $orderNumber = "Z" . $orderNumber;
                $data["cztime"] = date('Y-m-d H:i:s');
                $data["ddcode"] = $orderNumber;
                $data["tqktype"] = "10031";
                M::create($data);
                M::SendZadanNum($data['serverid'],$data['actorid'],$coin);
                M::SendEmail($data['serverid'], "盲盒特权卡发放！", "您的盲盒月卡已发放，请注意查收！", $data['actorid']);

            }else if ($data["tqktype"] == "10041") {

                $onet = ["50", "100", "200"];

                foreach ($onet as $value) {
                    $orderNumber = time() . rand(1000, 9999);
                    $orderNumber = "Z" . $orderNumber;
                    $data["cztime"] = date('Y-m-d H:i:s');
                    $data["ddcode"] = $orderNumber;
                    if ($value=="50") {
                        $data["tqktype"] = "10011";
                        if ($data["ismanyvalue"] == "1") {
                            $coin = (400 * intval($data["cardnum"])) . '';
                            $data["jine"] = (50 * intval($data["cardnum"])) . '';
                            $data["symonth"] = (30 * intval($data["cardnum"])) . '';
                        }else{
                            $coin = 400;
                            $data["jine"] = "50";
                            $data["symonth"] = "30";
                        }
                    }else if($value=="100"){
                        $data["tqktype"] = "10021";
                        if ($data["ismanyvalue"] == "1") {
                            $coin = (800 * intval($data["cardnum"])) . '';
                            $data["jine"] = (100 * intval($data["cardnum"])) . '';
                            $data["symonth"] = (30 * intval($data["cardnum"])) . '';
                        }else{
                            $coin = 800;
                            $data["jine"] = "100";
                            $data["symonth"] = "30";
                        }
                    }else if($value=="200"){
                        $data["tqktype"] = "10031";
                        if ($data["ismanyvalue"] == "1") {
                            $coin = (1600 * intval($data["cardnum"])) . '';
                            $data["jine"] = (200 * intval($data["cardnum"])) . '';
                            $data["symonth"] = (30 * intval($data["cardnum"])) . '';
                        }else{
                            $coin = 1600;
                            $data["jine"] = "200";
                            $data["symonth"] = "30";
                        }
                    }
                    M::create($data);
                    M::SendZadanNum($data['serverid'],$data['actorid'],$coin);
                }
                M::SendEmail($data['serverid'], "盲盒特权卡发放！", "您的盲盒月卡已发放，请注意查收！", $data['actorid']);
            } else {
                $data["jine"] = $data["tqktype"];
                //是否多选  1是  2否
                if ($data["ismanyvalue"] == "1") {
                    $data["symonth"] = (30 * intval($data["cardnum"])) . '';
                    //是否立得   1否  2是
                    if ($data["iscurrmvalue"] == "2") {
                        //针对后面添加的超级卡  发送对应物品  之前的旧三卡不参与
                        if ($data["tqktype"] == "998" || $data["tqktype"] == "1998" || $data["tqktype"] == "4998") {
                            if ($data["tqktype"] == "998") {
                                $newjine = intval($data["cardnum"]) * 500;
                                M::accrualPlayerRmb($data['actorname'], $data['serverid'], $newjine);
                            } else if ($data["tqktype"] == "1998") {
                                $newjine = intval($data["cardnum"]) * 1000;
                                M::accrualPlayerRmb($data['actorname'], $data['serverid'], $newjine);
                            } else if ($data["tqktype"] == "4998") {
                                $newjine = intval($data["cardnum"]) * 2500;
                                M::accrualPlayerRmb($data['actorname'], $data['serverid'], $newjine);
                            }
                            M::sendTQKEmail($data['serverid'], $data['actorid'], $data["tqktype"], intval($data["cardnum"]), $data["actorname"]);
                        } else {
                            $newjine = intval($data["jine"]) * intval($data["cardnum"]);
                            M::accrualPlayerRmb($data['actorname'], $data['serverid'], $newjine . '');
                        }
                    }
                } else {
                    $data["symonth"] = "30";
                    //是否立得  1否  2是
                    if ($data["iscurrmvalue"] == "2") {

                        //针对后面添加的超级卡  发送对应物品  之前的旧三卡不参与
                        if ($data["tqktype"] == "998" || $data["tqktype"] == "1998" || $data["tqktype"] == "4998") {
                            if ($data["tqktype"] == "998") {
                                M::accrualPlayerRmb($data['actorname'], $data['serverid'], '500');
                            } else if ($data["tqktype"] == "1998") {
                                M::accrualPlayerRmb($data['actorname'], $data['serverid'], '1000');
                            } else if ($data["tqktype"] == "4998") {
                                M::accrualPlayerRmb($data['actorname'], $data['serverid'], '2500');
                            }
                            M::sendTQKEmail($data['serverid'], $data['actorid'], $data["tqktype"], 1, $data["actorname"]);
                        } else {
                            M::accrualPlayerRmb($data['actorname'], $data['serverid'], $data["jine"]);
                        }
                    }
                }
                M::create($data);
                M::SendEmail($data['serverid'], "特权卡发放提示！", "您的特权卡已发放，请注意查收！", $data['actorid']);
            }
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    // 编辑
    public static function goEdit($data, $id)
    {
        $data['id'] = $id;
        //验证
        $validate = new V;
        if (!$validate->scene('edit')->check($data))
            return ['msg' => $validate->getError(), 'code' => 201];
        try {
            M::update($data);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    // 状态
    public static function goStatus($data, $id)
    {
        $model =  M::find($id);
        if ($model->isEmpty())  return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->save([
                'status' => $data,
            ]);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }
    // 删除
    public static function goRemove($id)
    {
        // $model = M::find($id);
        // if ($model->isEmpty()) return ['msg'=>'数据不存在','code'=>201];
        // try{
        //    $model->delete();
        // }catch (\Exception $e){
        //     return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        // }
        $data['id'] = $id;
        $data['status'] = '0';
        try {
            M::update($data);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    // 批量删除
    public static function goBatchRemove($ids)
    {
        // if (!is_array($ids)) return ['msg'=>'数据不存在','code'=>201];
        // try{
        //     M::destroy($ids);
        // }catch (\Exception $e){
        //     return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        // }
        if (!is_array($ids)) return ['msg' => '数据不存在', 'code' => 201];

        // 要修改的数据
        $updateData = [
            'status' => '0', // 修改的内容，这里与 goRemove 方法相同
        ];

        try {
            M::whereIn('id', $ids)->update($updateData);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    // 获取列表
    public static function goRecycle()
    {
        if (Request::isPost()) {
            $ids = Request::param('ids');
            if (!is_array($ids)) return ['msg' => '参数错误', 'code' => '201'];
            try {
                if (Request::param('type')) {
                    $data = M::onlyTrashed()->whereIn('id', $ids)->select();
                    foreach ($data as $k) {
                        $k->restore();
                    }
                } else {
                    M::destroy($ids, true);
                }
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
            return ['msg' => '操作成功'];
        }
        //按用户名
        $where = [];
        $limit = input('get.limit');

        //按查找
        if ($serverid = input("serverid")) {
            $where[] = ["serverid", "like", "%" . $serverid . "%"];
        }
        //按查找
        if ($actorname = input("actorname")) {
            $where[] = ["actorname", "like", "%" . $actorname . "%"];
        }
        //按查找
        if ($actoraccount = input("actoraccount")) {
            $where[] = ["actoraccount", "like", "%" . $actoraccount . "%"];
        }
        //按充值类型查找
        if ($cztype = input("cztype")) {
            $where[] = ["cztype", "like", "%" . $cztype . "%"];
        }
        //按状态：0.支付失败 1.支付成功查找
        if ($status = input("status")) {
            $where[] = ["status", "like", "%" . $status . "%"];
        }
        //按特权卡类型 0. 50   1. 100   2. 300查找
        if ($tqktype = input("tqktype")) {
            $where[] = ["tqktype", "like", "%" . $tqktype . "%"];
        }
        //按特权剩余天数查找
        if ($symonth = input("symonth")) {
            $where[] = ["symonth", "like", "%" . $symonth . "%"];
        }
        $list = M::onlyTrashed()->where($where)->paginate($limit);
        return ['code' => 0, 'data' => $list->items(), 'extend' => ['count' => $list->total(), 'limit' => $limit]];
    }
}
